﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    enum Status { Xem, Them, Xoa, Sua };
    public partial class frmDanhMucLoaiPhong : Form
    {
        public frmDanhMucLoaiPhong()
        {
            InitializeComponent();
        }

        LoaiPhongDTO lpDTO = null;
        Status TrangThai = Status.Xem;

        //Hiển thị lên DataGripView
        private void LoadData()
        {
            loaiPhongDTOBindingSource.DataSource = LoaiPhongBUS.SelectAll() ;
            loaiPhongDTODataGridView.Enabled = true;

            btnPhongMoi.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnLuu.Enabled = false;
        }

        private void AnHienTxt(Boolean t)
        {
            txtTenLoaiPhong.ReadOnly = t;
            txtDonGia.ReadOnly = t;
        }
        private void frmDanhMucLoaiPhong_Load(object sender, EventArgs e)
        {
            LoadData();
            AnHienTxt(true);
        }

        private void btnPhongMoi_Click(object sender, EventArgs e)
        {
            lpDTO = loaiPhongDTOBindingSource.AddNew() as LoaiPhongDTO;
            TrangThai = Status.Them;

            loaiPhongDTODataGridView.Enabled = false;
            btnPhongMoi.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            AnHienTxt(false);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            lpDTO = loaiPhongDTOBindingSource.Current as LoaiPhongDTO;
            TrangThai = Status.Sua;

            loaiPhongDTODataGridView.Enabled = false;
            btnPhongMoi.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            AnHienTxt(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string strKQ = null;
            lpDTO = loaiPhongDTOBindingSource.Current as LoaiPhongDTO;
            if (MessageBox.Show("Bạn có chắc xóa", "Thông tin", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                strKQ = LoaiPhongBUS.Xoa(lpDTO);
                if (strKQ == "True")
                {
                    loaiPhongDTOBindingSource.RemoveCurrent();

                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            string strKQ = null;
            if (TrangThai == Status.Them)
            {
                strKQ = LoaiPhongBUS.Them(lpDTO);
                if (strKQ == "True")
                {
                    lpDTO = null;
                    TrangThai = Status.Xem;
                    LoadData();
                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }
            if (TrangThai == Status.Sua)
            {
                strKQ = LoaiPhongBUS.CapNhat(lpDTO);
                if (strKQ == "True")
                {
                    lpDTO = null;
                    TrangThai = Status.Xem;
                    LoadData();
                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult dlgHoiThoat;
            dlgHoiThoat = MessageBox.Show("Bạn có chắc thoát??", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dlgHoiThoat == DialogResult.Yes)
                this.Close();
        }
    }
}
